// 获取应用实例
const app = getApp();

// 初始化数据库
const db = wx.cloud.database();
const uimg = db.collection("uimg");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    theme: 1,
    openid: "",
    o_openid: "",
    memorialList: [],
    isLoading: false,

    navHeight: app.globalData.navBarHeight,
    menuHeight: app.globalData.menuHeight,
    menuRight: app.globalData.menuRight,
    menuTop: app.globalData.menuTop,
  },

  // 获取数据
  getMemorData(openid) {
    const that = this;
    wx.showLoading({
      title: "Loading...",
    });
    uimg.where({ _openid: openid }).get({
      success: (res) => {
        const myList = res.data[0].memorial;
        const o_openid = res.data[0].o_openid;
        if (o_openid == "") {
          wx.showModal({
            title: "绑定提醒",
            content: "你还没有绑定情侣，是否现在绑定？",
            confirmText: "去绑定",
            confirmColor: "#a5646d",
            success: (result) => {
              if (result.confirm) {
                wx.navigateTo({
                  url: "/packageMine/pages/invitecode/invitecode",
                });
              } else if (result.cancel) {
                wx.navigateBack({
                  delta: 1,
                });
              }
            },
          });
          return;
        }
        that.setData({
          memorialList: myList,
          isLoading: false,
        });
      },
      fail: () => {
        wx.showToast({
          title: "加载失败",
          icon: "none",
        });
      },
      complete: () => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      },
    });
  },

  // 添加
  addMemorial() {
    wx.navigateTo({ url: "/packageMine/pages/invitecode/invitecode" });
  },

  // 删除
  onClose(e) {
    const that = this;
    let { openid, o_openid, memorialList } = that.data;
    const { time } = e.currentTarget.dataset;
    const { position, instance } = e.detail;
    switch (position) {
      case "left":
      case "cell":
        instance.close();
        break;
      case "right":
        wx.showModal({
          title: "提示",
          content: "确定删除此条纪念日嘛？",
          success: (result) => {
            if (result.confirm) {
              memorialList = memorialList.filter((i) => i.handleTime != time);
              uimg.where({ _openid: openid }).update({
                data: {
                  memorial: memorialList,
                },
                success: () => {
                  uimg.where({ _openid: o_openid }).update({
                    data: {
                      memorial: memorialList,
                    },
                    success: () => {
                      wx.showToast({
                        title: "已删除",
                        icon: "none",
                      });
                    },
                    fail: () => {
                      wx.showToast({
                        title: "删除失败",
                        icon: "none",
                      });
                    },
                    complete: () => {
                      that.getMemorData(openid);
                    },
                  });
                },
                fail: () => {
                  wx.showToast({
                    title: "删除失败",
                    icon: "none",
                  });
                  that.getMemorData(openid);
                },
              });

              instance.close();
            }
          },
          fail: () => { },
          complete: () => {
            instance.close();
          },
        });
        break;
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) { },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const openid = wx.getStorageSync("openid");
    const o_openid = wx.getStorageSync("o_openid");
    this.setData({
      theme: app.globalData.theme,
      openid,
      o_openid,
    });
    this.getMemorData(openid);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    const { openid } = this.data;
    this.setData({
      isLoading: true,
    });
    wx.showLoading({
      title: "Loading...",
    });
    this.getMemorData(openid);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { },
});
